package cn.com.nes.site.service.crm.opportunity.impl;

import cn.com.nes.common.exception.BusinessException;
import cn.com.nes.common.util.id.IdUtils;
import cn.com.nes.mybatis.agent.crm.entity.OpportunityStaff;
import cn.com.nes.mybatis.agent.crm.mapper.OpportunityStaffMapper;
import cn.com.nes.site.entity.em.DeleteEnum;
import cn.com.nes.site.service.crm.opportunity.OpportunityStaffService;
import cn.com.nes.site.service.crm.opportunity.bo.OpportunityStaffBO;
import cn.com.nes.site.service.crm.opportunity.dto.OpportunityStaffDTO;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class OpportunityStaffServiceImpl extends ServiceImpl<OpportunityStaffMapper, OpportunityStaff> implements OpportunityStaffService {

    @Override
    public List<OpportunityStaffDTO> getOpportunityStaffDtoList(OpportunityStaffBO opportunityStaffBo) {
        return this.baseMapper.getOpportunityStaffDtoList(opportunityStaffBo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OpportunityStaff saveStaff(OpportunityStaffBO opportunityStaffBO) throws Exception {
        log.debug("商机保存联系人，商机ID：{}", opportunityStaffBO.getOpportunityUuid());
        OpportunityStaff opportunityStaff = new OpportunityStaff();
        opportunityStaff.setOpportunityId(opportunityStaffBO.getOpportunityUuid());
        opportunityStaff.setCustomerStaffId(StrUtil.isNotEmpty(opportunityStaffBO.getCustomerStaffId()) ? Long.parseLong(opportunityStaffBO.getCustomerStaffId()) : null);
        opportunityStaff.setDeleted(String.valueOf(DeleteEnum._0.getKey()));
        if (ObjectUtil.isNotNull(this.baseMapper.selectOne(new QueryWrapper<>(opportunityStaff)))) {
            throw new BusinessException("联系人已添加，不可重复添加！");
        }

        OpportunityStaff staff = getStaffByUuid(opportunityStaffBO.getUuid());
        BeanUtils.copyProperties(opportunityStaffBO, staff);
        staff.setCustomerId(opportunityStaffBO.getCustomerUuid());
        staff.setOpportunityId(opportunityStaffBO.getOpportunityUuid());
        staff.setCustomerStaffId(StrUtil.isNotEmpty(opportunityStaffBO.getCustomerStaffId()) ? Long.parseLong(opportunityStaffBO.getCustomerStaffId()) : null);
        staff.setIsHousehold(opportunityStaffBO.getIsHousehold());
        if (StrUtil.isEmpty(staff.getUuid())) {
            staff.setUuid(IdUtils.uuid());
            staff.setCreateUid(String.valueOf(opportunityStaffBO.getOperateUserId()));
            staff.setCreateTime(opportunityStaffBO.getOperateDate());
            staff.setDeleted(String.valueOf(DeleteEnum._0.getKey()));
        }
        this.saveOrUpdate(staff);

        return staff;
    }

    @Override
    public Page<OpportunityStaffDTO> getStaffPage(OpportunityStaffBO bo) {
        Page<OpportunityStaffDTO> page = new Page<>(bo.getPageid(), bo.getPagesize());
        List<OpportunityStaffDTO> staffDTOS = this.baseMapper.getStaffPage(page, bo);
        page.setRecords(staffDTOS);
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteStaff(OpportunityStaffBO opportunityStaffBO) throws Exception {
        OpportunityStaff staff = getStaffByUuid(opportunityStaffBO.getUuid());
        staff.setDeleted(String.valueOf(DeleteEnum._1.getKey()));
        this.saveOrUpdate(staff);
    }

    private OpportunityStaff getStaffByUuid(String uuid) {
        if (StrUtil.isEmpty(uuid)) {
            return new OpportunityStaff();
        }
        OpportunityStaff opportunityStaff = this.baseMapper.selectById(uuid);
        return ObjectUtil.isNull(opportunityStaff) ? new OpportunityStaff() : opportunityStaff;
    }

}
